﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Netfraction.Network.Protocol.BASE.Handler
{
    public class HandlerCTM : ProtocolMessageActionHandler
    {
        public HandlerCTM()
            : base()
        {
            mSupportedConnectionStates = new List<RemoteMachine.ConnectionState>();
        }

        #region ProtocolMessageActionHandler Members

        public override string ProtocolActionCode
        {
            get { return "CTM"; }
        }

        public override bool HandleProtocolMessage(RemoteMachine.Node node, ProtocolMessage message, IEnumerable<ProtocolFeature> extenders)
        {
            if (node is RemoteMachine.Hub)
            {
                Console.WriteLine("in CTM handler");
                if (message.Parameters.Count == 3)
                {
                    //message.GETParameterOptions = DecodeParameters(message.GETParameterOptions);
                    //(node as RemoteMachine.Hub).SessionId = message.GETParameterOptions[0].ParameterValue;
                    Console.WriteLine("Source SID: {0}", message.SessionId);
                    Console.WriteLine("Protocol: {0}, Port: {1}, Token: {2}", 
                        message.Parameters[0].ParameterValue,
                        message.Parameters[1].ParameterValue,
                        message.Parameters[2].ParameterValue);

                    string protocol = message.Parameters[0].ParameterValue;
                    string port = message.Parameters[1].ParameterValue;
                    string token = message.Parameters[2].ParameterValue;

                    if (protocol == "ADC/1.0" || protocol == "ADC/0.10")
                    {
                        LocalMachine.Client client = LocalMachine.Client.GetInstance();
                        lock (client)
                        {
                            RemoteMachine.Peer p = client.Peers.Where(c => c.SessionId == message.SessionId).First();
                            p.Identity.Token = message.Parameters[2].ParameterValue;
                            System.Net.IPEndPoint ep = new System.Net.IPEndPoint(p.Identity.IPv4Address, Convert.ToInt32(message.Parameters[1].ParameterValue));
                            p.Connection.Connect(ep);
                            /*ProtocolMessageFactory factory = new Factory.FactoryINF();

                            Dictionary<int, object> factoryParams = new Dictionary<int, object>();
                            factoryParams.Add((int)Factory.FactoryINF.GETParameterOptions.SessionId, (node as RemoteMachine.Hub).SessionId);
                            factoryParams.Add((int)Factory.FactoryINF.GETParameterOptions.ClientId, client.Identity.ClientId);
                            // Send Client Information to Hub
                            (node as RemoteMachine.Hub).Send(factory.Create(ProtocolMessage.MessageTypeEnum.Broadcast, factoryParams).ToString());*/
                        }
                    }
                    else
                    {
                        LocalMachine.Client client = LocalMachine.Client.GetInstance();
                        lock (client)
                        {
                            ProtocolMessageFactory factory = new Factory.FactorySTA();
                            Dictionary<int, object> factoryParams = new Dictionary<int, object>();
                            factoryParams.Add((int)Factory.FactorySTA.ParameterEnum.SessionID, client.Peers[client.Identity.ClientId].SessionId);
                            factoryParams.Add((int)Factory.FactorySTA.ParameterEnum.InitiatorSID, message.SessionId);
                            factoryParams.Add((int)Factory.FactorySTA.ParameterEnum.Severity, Factory.FactorySTA.StatusSeverity.FatalError);
                            factoryParams.Add((int)Factory.FactorySTA.ParameterEnum.Code, Factory.FactorySTA.StatusErrorCode.TransferProtocolUnsupported);
                            factoryParams.Add((int)Factory.FactorySTA.ParameterEnum.Token, token);
                            factoryParams.Add((int)Factory.FactorySTA.ParameterEnum.Protocol, protocol);
                            (node as RemoteMachine.Hub).Connection.Send(factory.Create(ProtocolMessage.MessageTypeEnum.Direct, factoryParams).ToString());
                        }
                    }
                }
                return true;
            }
            else
            {
                return false;
            }
        }

        #endregion
    }
}
